package com.exam.controller;

import com.exam.entity.Administrator;
import com.exam.entity.Student;
import com.exam.service.UserCheckService;
import com.exam.util.ZLConstants;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class LoginController {

    @Resource
    private UserCheckService userCheckService;

    @RequestMapping("/login")
    public Object login(String op, String verifycode, Administrator admin, Student stu, HttpSession session){
        if(op!=null & !"".equals(op)){
            if("admin".equals(op)){
                System.out.println(session.getAttribute(ZLConstants.VERIFYCODE));
                if(!verifycode.equalsIgnoreCase(session.getAttribute(ZLConstants.VERIFYCODE).toString())){//先判断验证码是否正确
                    return "0";
                }
                if(userCheckService.checkAdmin(admin.getAname(),admin.getApwd())>0){//检验管理员是否登录成功
                    admin.setApwd("");//将密码置空
                    session.setAttribute(ZLConstants.USER, admin);//将登录成功的用户存入session
                    if(admin.getAutologin()!=null){//如果点了自动登录
                        session.setMaxInactiveInterval(14*24*60*60*1000);//自动登录14天
                    }
                    return "1";
                }
                return "0";
            }else if("stu".equals(op)){
//                暂时去掉验证码验证
                if(!verifycode.equalsIgnoreCase(session.getAttribute(ZLConstants.VERIFYCODE).toString())){//先判断验证码是否正确
                    return "0";
                }
                List<Student> res = userCheckService.checkStudent(stu.getSno(),stu.getSpwd());
                if(res.size()>0){//登录成功
                    stu = res.get(0);
                    stu.setSpwd("");
                    if(!"".equals(stu.getSavator())){
                        stu.setSavator(stu.getSavator());
                    }
                    if(stu.getAutologin()!=null){//如果点了自动登录
                        session.setMaxInactiveInterval(14*24*60*60*1000);//自动登录14天
                        System.out.println("开启自动登录");
                    }
                    session.setAttribute(ZLConstants.USER, stu);
                    return "1";
                }
                return "0";
            }else if("logout".equals(op)){
                session.setMaxInactiveInterval(0);
                session.removeAttribute(ZLConstants.USER);//清除session中存储的对象
            }else if("stuCheck".equals(op)){
                Student student = (Student) session.getAttribute(ZLConstants.USER);
                String pwd = stu.getSpwd();//对比密码是否相同
                List<Student> res = userCheckService.checkStudent(student.getSno(),pwd);
                return res.size();
            }else if("stuModify".equals(op)){
                Student student = (Student) session.getAttribute(ZLConstants.USER);
                String pwd = stu.getSpwd();
                int result = userCheckService.modifyStuPwd(student.getSno(),pwd);
                return result;
            }
        }
        return "0";
    }

    @RequestMapping("/reset")
    public String resetPassword(HttpSession session,String sign,String sno){
        Long resetDate = (Long)(session.getAttribute("resetDate"));
        String resetSign = (String)session.getAttribute("resetSign");
        if(resetDate==null ||resetDate < new Date().getTime())
            return "Time Out!";
        if(sign == null || !sign.equals(resetSign))
            return "Sign Error!";
        if(userCheckService.modifyStuPwd(sno,"123456")!=0){
            return "Success";
        };
        return "Error";
    }
}
